﻿
(function () {
    appModule.controller('tenant.views.instructions.createOrEditInstructions', [
        '$scope', '$uibModalInstance', 'abp.services.app.instructions', 'instructionsId', 'FileUploader',
        function ($scope, $uibModalInstance, instructionsService, instructionsId, fileUploader) {
            var vm = this;
            
            vm.saving = false;
            vm.instructions = null;

            var fileIdArr = [];
            //文件上传
            vm.uploader = new fileUploader({
                url: abp.appPath + 'Profile/UploaderTemple',
                queueLimit: 1,
                autoUpload: true,
                removeAfterUpload: true,
                filters: [{
                    fn: function (item, options) {
                        return true;
                    }
                }],
            });
            vm.uploader.onSuccessItem = function (fileItem, response, status, headers) {
                fileIdArr.push(response);
                var filtersArr = [{ field: "id", operator: "eq", value: Number(instructionsId) }];
                for (var i = 0; i < fileIdArr.length; i++) {
                    filtersArr.push({ field: "fileId", operator: "eq", value: Number(fileIdArr[i]) });
                }
                var filter = {
                    logic: "or",
                    filters: filtersArr
                }
                vm.mainGrid.dataSource.filter(filter);
            };
            //列表数据源
            var urlFiles = "/api/services/app/instructions/";
            var mainGridDataSource = new kendo.data.DataSource({
                type: 'aspnetmvc-ajax',
                transport: {
                    read: {
                        url: urlFiles + "KendoGetInstructionsById",
                        contentType: "application/json",
                        type: "POST"
                    },
                    parameterMap: function (options, operation) {
                        return JSON.stringify(options);
                    }
                },
                pageSize: 10,
                serverPaging: true,
                serverFiltering: true,
                serverSorting: true,
                schema: {
                    data: function (data) {
                        if (data.success === true && data.result != null) {
                            return data.result.data;
                        }
                        return {};
                    },
                    total: function (data) {
                        if (data.success === true && data.result != null) {
                            return data.result.total;
                        }
                        return 0;
                    },
                    model: {
                        id: "id",
                        fields: {
                            id: { type: "number" },
                        }
                    }
                },
                filter: {
                    logic: "and",
                    filters: [
                        { field: "id", operator: "eq", value: instructionsId }
                    ]
                }
            });
            //列表输出
            vm.mainGridOptions = {
                dataSource: mainGridDataSource,
                columnMenu: true,
                reorderable: true,
                resizable: true,
                filterable: true,
                scrollable: true,
                selectable: "multiple",
                allowCopy: true,
                //height: 650,
                //change: function (e) {
                //    var selectedRows = this.select();
                //    for (var i = 0; i < selectedRows.length; i++) {
                //        vm.curFiles = this.dataItem(selectedRows[i]);
                //    }
                //},
                pageable: {
                    buttonCount: 5.0,
                    pageSize: 10,
                    pageSizes: [10, 20, 50, 100],
                    refresh: true
                },
                sortable: {
                    mode: "multiple"
                },
                messages: {
                    commands: {
                        excel: "导出"
                    }
                },
                columns: [
                    {
                        title: '文件名',
                        field: 'name'
                    },
                    {
                        title: '操作',
                        width: 120,
                        command: {
                            template: '<kendo-button ng-click="vm.deleteFiled(dataItem);" ><span class="k-icon k-delete"></span> 删除</kendo-button>'
                        }
                    }
                ]
            };

            //删除文件
            vm.deleteFiled = function (dataItem) {
                instructionsService.deleteFiled({ id: dataItem.id }).success(function () {
                    abp.notify.info('删除成功');
                    vm.mainGridOptions.dataSource.read();
                }).finally(function () {

                });
            };
            
            vm.save = function () {
                vm.saving = true;
                instructionsService.createOrUpdateInstructions({ instructions: vm.instructions, fileIds: fileIdArr }).success(function () {
                    abp.notify.info(app.localize('SavedSuccessfully'));
                    $uibModalInstance.close();
                }).finally(function () {
                    vm.saving = false;
                });
            };

            vm.cancel = function () {
                $uibModalInstance.dismiss();
            };
            function init() {
                instructionsService.getInstructionsForEdit({
                    id: instructionsId
                }).success(function (result) {
                    vm.instructions = result.instructions;
                });
            }

            init();
        }
    ]);
})();
